var clickModel, currentFloor;
var isSelectS,
  isSelectD,
  isSelectQT,
  isSelectZP,
  isSelectKTF,
  isSelectKTS,
  isSelectCamera,
  isSelectSmoke,
  isSelectFire;
const JZ_COLOR = 0x5d6677,
  JG_COLOR = 0xffffff,
  S_COLOR = 0xe44f2e,
  D_COLOR = 0x6098e6,
  KTF_COLOR = 0x83de95,
  KTS_COLOR = 0xcf70d8,
  QT_COLOR = 0xe0c93b,
  ZP_COLOR = 0xea2121,
  CAMERA_COLOR = 0x4ace97;
const COLOR_MAP = {
  JZ: JZ_COLOR,
  JG: JG_COLOR,
  S: S_COLOR,
  D: D_COLOR,
  KTF: KTF_COLOR,
  KTS: KTS_COLOR,
  QT: QT_COLOR,
  ZP: ZP_COLOR,
  CAMERA01: CAMERA_COLOR,
  CAMERA02: CAMERA_COLOR,
  CAMERA03: CAMERA_COLOR,
};
const POSITION_MAP = {
  JZ: { x: 500, y: 300, z: 500 },
  JG: { x: 500, y: 250, z: 500 },
  S: { x: 540, y: 300, z: 460 },
  D: { x: 480, y: 300, z: 520 },
  KTF: { x: 600, y: 300, z: 400 },
  KTS: { x: 550, y: 300, z: 450 },
  QT: { x: 510, y: 300, z: 490 },
  ZP: { x: 580, y: 300, z: 420 },
};

$(document).ready(function () {
  loadCity().then(loadJZ).then(hideMask).then(showHome);

  $.getJSON("./resource/json/data.json", (data) => {
    bimData = data;
  });
});

function tm(mesh, opacity) {
  if (mesh && mesh.material) {
    mesh.material.transparent = true;
    mesh.material.opacity = opacity;
    mesh.material.visible = !!opacity;
    if (mesh.material instanceof Array) {
      mesh.material.forEach((m) => {
        m.transparent = true;
        m.opacity = opacity;
        m.visible = !!opacity;
      });
    }
  }
}

function showMask() {
  $(".mask-wrapper").show();
}

function hideMask() {
  return new Promise((resolve) => {
    $(".mask-wrapper").hide();
    resolve && resolve();
  });
}

function showDetail(id, isIncident) {
  if (isIncident) {
    top.storageserver && top.storageserver.incidentInfo(id);
  } else {
    top.storageserver && top.storageserver.deviceInfo(id);
  }
}

function showHome() {
  top.storageserver && top.storageserver.home3d();
}

function showMessage(msg) {
  $(".mask-text").html(msg);
}

function loadJZ(isChecked) {
  let p = new Promise((resolve) => resolve(isChecked));
  if (isChecked) {
    if (loaded.JZ) {
      var model = loaded.JZ;
      tm(model, 1);
    } else {
      p = loadBuilding();
    }
  } else {
    var model = loaded.JZ;
    tm(model, 0);
  }
  return p;
}

function loadS(isChecked) {
  let p = new Promise((resolve) => resolve(isChecked));
  if (isChecked) {
    if (loaded.S) {
      var model = loaded.S;
      tm(model, 0.5);
    } else {
      p = loadModel("S", COLOR_MAP["S"]);
    }
  } else {
    var model = loaded.S;
    tm(model, 0);
  }
  return p;
}

function loadD(isChecked) {
  let p = new Promise((resolve) => resolve(isChecked));
  if (isChecked) {
    if (loaded.D) {
      var model = loaded.D;
      tm(model, 0.5);
    } else {
      p = loadModel("D", COLOR_MAP["D"]);
    }
  } else {
    var model = loaded.D;
    tm(model, 0);
  }
  return p;
}

function loadKTF(isChecked) {
  let p = new Promise((resolve) => resolve(isChecked));
  if (isChecked) {
    if (loaded.KTF) {
      var model = loaded.KTF;
      tm(model, 0.5);
    } else {
      p = loadModel("KTF", COLOR_MAP["KTF"]);
    }
  } else {
    var model = loaded.KTF;
    tm(model, 0);
  }
  return p;
}

function loadKTS(isChecked) {
  let p = new Promise((resolve) => resolve(isChecked));
  if (isChecked) {
    if (loaded.KTS) {
      var model = loaded.KTS;
      tm(model, 0.5);
    } else {
      p = loadModel("KTS", COLOR_MAP["KTS"]);
    }
  } else {
    var model = loaded.KTS;
    tm(model, 0);
  }
  return p;
}

function loadQT(isChecked) {
  let p = new Promise((resolve) => resolve(isChecked));
  if (isChecked) {
    if (loaded.QT) {
      var model = loaded.QT;
      tm(model, 0.5);
    } else {
      p = loadModel("QT", COLOR_MAP["QT"]);
    }
  } else {
    var model = loaded.QT;
    tm(model, 0);
  }
  return p;
}

function loadZP(isChecked) {
  let p = new Promise((resolve) => resolve(isChecked));
  if (isChecked) {
    if (loaded.ZP) {
      var model = loaded.ZP;
      tm(model, 0.5);
    } else {
      p = loadModel("ZP", COLOR_MAP["ZP"]);
    }
  } else {
    var model = loaded.ZP;
    tm(model, 0);
  }
  return p;
}

function loadFloor(floor) {
  // floor参数备用，目前只做一层
  return clipFloor();
}

function loadAll(isChecked) {
  return (
    loadS(isChecked)
      .then(loadD)
      .then(loadKTF)
      .then(loadKTS)
      .then(loadQT)
      .then(loadZP)
      // .then(loadRoom)
      .then(loadCameras)
      .then(loadJG)
      .then(loadJZ)
  );
}

function toggleFloor(floor, callback) {
  $(".floor-select").children().removeClass("selected");
  if (floor === 0) {
    // 取消进入楼层
    currentFloor = false;
    unclipFloor();
    $(".in-floor").hide();
    isSelectCamera = false;
    isSelectSmoke = false;
    isSelectFire = false;
    $(".floor-select").children().removeClass("selected");
  } else {
    // 进入指定楼层
    currentFloor = floor;
    unclipFloor(function () {
      loadFloor(floor).then(function () {
        callback && callback();
      });
      $(".in-floor").show();
    });
  }
  $(".floor-objects").hide();
  $(".in-floor").children().removeClass("selected");
  isSelectCamera = false;
  isSelectSmoke = false;
  isSelectFire = false;
  $($(".floor-select").children()[floor]).addClass("selected");
  top.storageserver && top.storageserver.hide();
}

function toggleS() {
  loadS(!isSelectS).then(hideMask);
  if (isSelectS) {
    $($(".in-building").children()[0]).removeClass("selected");
  } else {
    $($(".in-building").children()[0]).addClass("selected");
  }
  isSelectS = !isSelectS;
}

function toggleD() {
  loadD(!isSelectD).then(hideMask);
  if (isSelectD) {
    $($(".in-building").children()[1]).removeClass("selected");
  } else {
    $($(".in-building").children()[1]).addClass("selected");
  }
  isSelectD = !isSelectD;
}

function toggleQT() {
  loadQT(!isSelectQT).then(hideMask);
  if (isSelectQT) {
    $($(".in-building").children()[2]).removeClass("selected");
  } else {
    $($(".in-building").children()[2]).addClass("selected");
  }
  isSelectQT = !isSelectQT;
}

function toggleZP() {
  loadZP(!isSelectZP).then(hideMask);
  if (isSelectZP) {
    $($(".in-building").children()[3]).removeClass("selected");
  } else {
    $($(".in-building").children()[3]).addClass("selected");
  }
  isSelectZP = !isSelectZP;
}

function toggleKTF() {
  loadKTF(!isSelectKTF).then(hideMask);
  if (isSelectKTF) {
    $($(".in-building").children()[4]).removeClass("selected");
  } else {
    $($(".in-building").children()[4]).addClass("selected");
  }
  isSelectKTF = !isSelectKTF;
}

function toggleKTS() {
  loadKTS(!isSelectKTS).then(hideMask);
  if (isSelectKTS) {
    $($(".in-building").children()[5]).removeClass("selected");
  } else {
    $($(".in-building").children()[5]).addClass("selected");
  }
  isSelectKTS = !isSelectKTS;
}

function toggleCamera() {
  if (window.bimData && window.bimData.cameras) {
    var html = "";
    window.bimData.cameras.forEach((c) => {
      html += `<div data-id=${c.id}>${c.title}-${c.location}</div>`;
    });
    $(".floor-objects").html(html).css("marginTop", "-170px");
    $(".floor-objects")
      .children()
      .click(function () {
        loadFloor().then((_) => focus(this.getAttribute("data-id")));
        $(this).parent().children().removeClass("selected");
        $(this).addClass("selected");
      });
    $(".in-floor").children().removeClass("selected");
    if (!isSelectCamera) {
      $(".floor-objects").show();
      $($(".in-floor").children()[0]).addClass("selected");
    } else {
      $(".floor-objects").hide();
      $($(".in-floor").children()[0]).removeClass("selected");
    }
    isSelectSmoke = false;
    isSelectFire = false;
    isSelectCamera = !isSelectCamera;
    top.storageserver && top.storageserver.hide();
  }
}

function toggleSmoke() {
  if (window.bimData && window.bimData.smokes) {
    var html = "";
    window.bimData.smokes.forEach((s) => {
      html += `<div data-id=${s.id}>${s.title}-${s.location}</div>`;
    });
    $(".floor-objects").html(html).css("marginTop", "-120px");
    $(".floor-objects")
      .children()
      .click(function () {
        loadFloor().then((_) => focus(this.getAttribute("data-id")));
        $(this).parent().children().removeClass("selected");
        $(this).addClass("selected");
      });
    $(".in-floor").children().removeClass("selected");
    if (!isSelectSmoke) {
      $(".floor-objects").show();
      $($(".in-floor").children()[1]).addClass("selected");
    } else {
      $(".floor-objects").hide();
      $($(".in-floor").children()[1]).removeClass("selected");
    }
    isSelectCamera = false;
    isSelectFire = false;
    isSelectSmoke = !isSelectSmoke;
    top.storageserver && top.storageserver.hide();
  }
}

function toggleFire() {
  if (window.bimData && window.bimData.fires) {
    var html = "";
    window.bimData.fires.forEach((f) => {
      html += `<div data-id=${f.id}>${f.title}-${f.location}</div>`;
    });
    $(".floor-objects").html(html).css("marginTop", "-60px");
    $(".floor-objects")
      .children()
      .click(function () {
        loadFloor().then((_) => focus(this.getAttribute("data-id")));
        $(this).parent().children().removeClass("selected");
        $(this).addClass("selected");
      });
    $(".in-floor").children().removeClass("selected");
    if (!isSelectFire) {
      $(".floor-objects").show();
      $($(".in-floor").children()[2]).addClass("selected");
    } else {
      $(".floor-objects").hide();
      $($(".in-floor").children()[2]).removeClass("selected");
    }
    isSelectCamera = false;
    isSelectSmoke = false;
    isSelectFire = !isSelectFire;
    top.storageserver && top.storageserver.hide();
  }
}

function focusIncident(id) {
  if (window.bimData && window.bimData.incidents) {
    let incidents = window.bimData.incidents.filter((i) => i.id === id);
    if (incidents.length) {
      let incident = incidents[0];
      focusBuilding(function () {
        toggleFloor(incident.floor, function () {
          focus(incident.device, true);
        });
      });
    }
  }
}
